﻿//创建时间: 2010-12-15
//创建作者: 李城
//功能说明: 更新广告
//最后修改: 2010-12-15
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.CMS.Web.UI;
using TRPOP.CMS.Model;
using TRPOP.CMS.Business;
using TRPOP.Common;
using TRPOP.Config;

namespace TRPOP.CMS.Web.Admin
{
    public partial class advertisement_update : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_strPagePermissionExtraKey = "Plugin_Advertisement";
        }
        #endregion

        #region 页面变量
        protected AdvscontentManage mAdvscontentManage = new AdvscontentManage();
        #endregion

        #region 页面事件


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
            }
        }

        /// 返回操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ViewState["RequestUrl"] == null
                || ViewState["RequestUrl"].ToString().ToLower().Contains("lefttree.aspx")
                || ViewState["RequestUrl"].ToString().ToLower().Contains("advertisement_update.aspx")
                )
            {
                base.Redirect("../Admin_Main.aspx");
            }
            else
                base.Redirect(ViewState["RequestUrl"].ToString());
        }

        #endregion

        #region 页面方法

        /// <summary>
        /// 操作导航
        /// </summary>
        /// <summary>
        public void LoadBase()
        {

            #region UrlReferrer
            try
            {
                ViewState["RequestUrl"] = Request.UrlReferrer.ToString();
            }
            catch { }

            if (TRPOP.Common.RequestHelper.RequestString("refer").IsEmpty() == false)
            {
                ViewState["RequestUrl"] = TRPOP.Common.RequestHelper.RequestString("refer");
            }
            #endregion

            if (TRPOP.Common.RequestHelper.RequestString("AjaxPostBack").IsEmpty())
            {
                //过期的广告 
                List<AdvscontentInfo>  oNotValidList = mAdvscontentManage.GetNotValidList();
                string strDelids = "";
                bool blnDelids = false;
                if (oNotValidList == null || oNotValidList.Count == 0)
                    spaceusedAdDel.InnerHtml = "无";
                else
                {
                    blnDelids = true;
                    foreach (AdvscontentInfo tempInfo in oNotValidList)
                    {
                        strDelids += "," + tempInfo.AdID.ToString();
                    }
                    if (strDelids.Length > 0)
                        strDelids = strDelids.Remove(0, 1);
                }

                //更新js文件的 
                List<AdvscontentInfo> oValidList = mAdvscontentManage.GetValidAndStaticList();
                string strUpdateids = "";
                bool blnUpdateids = false;
                if (oValidList == null || oValidList.Count == 0)
                    spaceusedAdUpdate.InnerHtml = "无";
                else
                {
                    blnUpdateids = true;
                    foreach (AdvscontentInfo tempInfo in oValidList)
                    {
                        strUpdateids += "," + tempInfo.AdID.ToString();
                    }
                    if (strUpdateids.Length > 0)
                        strUpdateids = strUpdateids.Remove(0, 1);
                }

                //删除的js
                ltJS.Text = string.Format(ltJS.Text, new object[] { 
                                                                    strDelids,
                                                                    strUpdateids,
                                                                    blnDelids.ToString().ToLower(),
                                                                    blnUpdateids.ToString().ToLower()
                                                                    }
                                          );
            }
            else
            {
                DoAjaxPostBack();
            }
        }

        //异步操作
        public void DoAjaxPostBack()
        {
            Response.ClearHeaders();
            Response.ClearContent();
            string strType = TRPOP.Common.RequestHelper.RequestString("type");
            string strExtras = TRPOP.Common.RequestHelper.RequestString("ids").Trim();
            int intID = 0;
            switch (strType)
            {
                case "Del":
                    int.TryParse(strExtras, out intID);
                    mAdvscontentManage.DeleteJSFile(intID);
                    Response.Write("OK");
                    base.End();
                    break;
                case "Up":
                    mAdvscontentManage.UpdateJSFile(intID);
                    Response.Write("OK");
                    base.End();
                    break;
                default:
                    break;
            }
        }

        #endregion
    }
}
